package com.lslk.othello;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.lslk.othello.models.Game;
import com.lslk.othello.models.Preferences;
import com.lslk.othello.models.Position.Occupation;

public class ActivityOthello extends Activity {
	public Game game;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        game=new Game(this);
    }
    public void start_game(View v){
		if(!game.isStarted){
			game.startGame();
			((Button)v).setText("Pause Game");
		} else{
			if(game.isPaused){
				try{
				if(game.aiThread!=null && ((Preferences.aiPlaysBlack(this) && game.blackTurn) || ((Preferences.aiPlaysWhite(this)) && (!game.blackTurn))))
					game.aiThread.start();
				}catch(Exception e){}
				game.isPaused=false;
				((Button)v).setText("Pause Game");
			} else{
				game.isPaused=true;
				((Button)v).setText("Resume Game");
			}
		}
    }
    public void reset_game(View v){
		game.isPaused=true;//Stop the other thread
		this.startActivity(new Intent(this,ActivityOthello.class));
		this.finish();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	int base=Menu.FIRST; 
    	menu.add(base,base,base,"Opponent Moves")
    		.setIcon(android.R.drawable.ic_menu_directions);
    	menu.add(base,base+1,base+1,"Settings")
    		.setIcon(android.R.drawable.ic_menu_preferences);
    	menu.add(base,base+2,base+2,"Evaluate as Black")
			.setIcon(android.R.drawable.ic_menu_view);
    	menu.add(base,base+3,base+3,"Evaluate as White")
			.setIcon(android.R.drawable.ic_menu_view);
    	return true;
    } 
    @Override
    public void onBackPressed(){
    	super.onBackPressed();
    }
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
    	if (item.getItemId() == Menu.FIRST){
    		game.assignForOther();
    		return true;
    	}
    	if (item.getItemId() == Menu.FIRST+1){
    			this.startActivity(new Intent(this,ActivityPreferences.class));
    		return true;
    	}
    	if(item.getItemId() == Menu.FIRST+2){
    		game.evaluate(Occupation.BLACK);
    		return true;
    	}
    	if(item.getItemId() == Menu.FIRST+3){
    		game.evaluate(Occupation.WHITE);
    		return true;
    	}
    		
    	return true;
    }
}